package com.cgsd.heyu.ui;

import android.content.Intent;

import com.cgsd.heyu.R;
import com.cgsd.heyu.ui.home.fragment.WrapperSendFragment;
import com.cgsd.heyu.utils.GlideEngine;
import com.luck.picture.lib.PictureSelectionModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

import androidx.fragment.app.Fragment;

import static android.app.Activity.RESULT_OK;

/**
 * Author: sky on 2021/3/25 14:06
 * Email: xcode126@126.com
 * Desc:
 */
public abstract class BasePickerFragment extends WrapperSendFragment {

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    obtainMediaResult(PictureSelector.obtainMultipleResult(data));
                    break;
//                case PictureConfig.REQUEST_CAMERA:
//                    obtainMediaResult(PictureSelector.obtainMultipleResult(data));
//                    break;
            }
        }
    }

    /**
     * 选取图片
     *
     * @param maxSelectNum 选取图片的数量  多选时才生效
     * @param singleMode   是否单选
     * @param showCamera   是否显示拍照按钮
     * @param crop         是否剪裁
     * @param isCircleMode 剪裁是否显示圆形参考   用于裁剪圆形头像    开启裁剪时生效
     */
    public void showPictureSelector(Fragment fragment, int maxSelectNum, boolean singleMode,
                                    boolean showCamera, boolean crop, boolean isCircleMode, boolean isVideo) {
        PictureSelectionModel model = PictureSelector.create(fragment)
                .openGallery(isVideo ? PictureMimeType.ofVideo() : PictureMimeType.ofImage()) //查看类型  仅图片,可选择其它或全部
                .theme(R.style.picture_WeChat_style)
                .maxSelectNum(maxSelectNum) //最大图片选择数量
                .isCompress(true)
                .compressQuality(60)// 图片压缩后输出质量
                .minimumCompressSize(100)// 小于100kb的图片不压缩
//                .compressMaxKB(500)
                .imageSpanCount(4)// 每行显示个数
                .minSelectNum(1)// 最小选择数量
                .selectionMode(singleMode ?
                        PictureConfig.SINGLE : PictureConfig.MULTIPLE)// 单选 or 多选
                .imageEngine(GlideEngine.createGlideEngine())
                .isCamera(showCamera);// 是否显示拍照按钮
        if (crop) {
            model
                    .isEnableCrop(crop)             //开启裁剪
                    .cutOutQuality(90)// 裁剪输出质量 默认100
                    .hideBottomControls(false)  //是否隐藏裁剪工具栏
                    .circleDimmedLayer(isCircleMode)    //显示圆形参考图层
                    .withAspectRatio(1, 1);     //裁剪比例
        }
        model.forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    /**
     * 默认不压缩的选择图库
     *
     * @param maxSelectNum
     * @param singleMode
     * @param showCamera
     */
    public void showPictureSelector(Fragment fragment, int maxSelectNum, boolean singleMode, boolean showCamera) {
        showPictureSelector(fragment, maxSelectNum, singleMode, showCamera, false, false, false);
    }

    /**
     * 默认不压缩的选择图库
     *
     * @param maxSelectNum
     * @param singleMode
     * @param showCamera
     */
    public void showPictureSelector(Fragment fragment, int maxSelectNum, boolean singleMode, boolean showCamera, boolean isVideo) {
        showPictureSelector(fragment, maxSelectNum, singleMode, showCamera, false, false, isVideo);
    }


    /**
     * 获取到选择的内容的回调
     *
     * @param list
     */
    public abstract void obtainMediaResult(List<LocalMedia> list);


}
